﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SQLCE.Service;
using SQLCE.Entity;
using System.Threading;
using System.Globalization;

namespace SQLCE.Controllers
{
    public class HomeController : Controller
    {
        ProductManager _productManager;
        public HomeController(ProductManager productManager)
        {
            _productManager = productManager;
        }
        public ActionResult Index(int? id)
        {
            ViewBag.Message = "欢迎使用 ASP.NET MVC!";

            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Request.QueryString["culture"] ?? "en", false);


            var model = _productManager.GetProduct(null);
            if (id.HasValue)
                model = _productManager.GetProduct(i => i.ID == id.Value);
            return View("Index", "_LayoutGreen", model);
        }

        public ActionResult Edit(int? id)
        {
            if (!id.HasValue)
                return RedirectToAction("Index");
            return View(_productManager.GetProduct(id.Value));

        }

        [HttpPost]
        public ActionResult Edit(Product entity)
        {
            if (ModelState.IsValid)
            {
                _productManager.UpdateProduct(entity);
                return RedirectToAction("Index");
            }
            return View();
        }


        public ActionResult Delete(int? id)
        {
            if (id.HasValue)
                _productManager.DeleteProduct(id.Value);
            return RedirectToAction("Index");
        }

        public ActionResult About(int? grade, int? subject, int? version)
        {
            ViewBag.query = string.Format("grade:{0},subject:{1},verson{2}", grade, subject, version);
            return View();
        }

        public ActionResult About2(int? grade, int? subject)
        {
            ViewBag.query = string.Format("grade:{0},subject:{1}", grade, subject);
            return View();
        }
    }
}
